import axios from 'axios'
import cookies from '@/plugins/jsCookies'

// import qs from 'qs'

//创建axios
let service = axios.create({
  // baseURL: 'https://xgy.kals.dev:9453',
  baseURL: 'http://localhost:8023/api/',
  timeout: 10000,
  withCredentials: true
})
service.defaults.withCredentials = true
//添加请求拦截器
service.interceptors.request.use(function (config) {


  // 所有访问，添加header的Authorization字段，校验是否登录
  if (localStorage.getItem("auth")) {
    config.headers["Authorization"] = localStorage.getItem("auth")
  }

  return config;
}, function (error) {
  return Promise.reject(error);
})

// 响应拦截器
service.interceptors.response.use(success => {

  // 登录时添加
  if (success.status === 200) {
    if (success.config.url == "/sso/cpc/login" || success.config.url == "/sso/user/login") {
      localStorage.setItem("auth", success.headers.authorization)
    }
    return success.data
  }

  return success;

}, error => {
  // alert(error.response.data)
  console.log(error.response);
})

export default service